package cn.net.yato.picture.controller;

import cn.net.yato.picture.biz.SysBiz;
import cn.net.yato.picture.biz.UserBiz;
import cn.net.yato.picture.constant.CommParams;
import cn.net.yato.picture.entity.LoginLog;
import cn.net.yato.picture.entity.User;
import cn.net.yato.picture.util.email.EmailUtil;
import cn.net.yato.picture.util.encryption.DateTools;
import cn.net.yato.picture.util.phone.QcloudSmsUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author yato
 * @Date 2020/2/17 16:04
 * @Version 1.0
 */
@RestController
public class LoginController {

    @Resource
    private SysBiz sysBiz;
    @Resource
    private UserBiz userBiz;
    @Autowired
    private QcloudSmsUtil qcloudSmsUtil;
    @Autowired
    private EmailUtil emailUtil;

    @GetMapping("/login.html")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping("/logout")
    public void logout(HttpServletResponse response, HttpSession session) throws IOException {
        session.removeAttribute(CommParams.LOGIN_SESSION_KEY);
        SecurityUtils.getSubject().logout();
        response.sendRedirect("login.html");
    }

    /**
     * 用户名密码登录
     * @param userName
     * @param passWord
     * @param remember
     * @param session
     * @param ip
     * @param address
     * @return
     */
    @PostMapping("/doLogin.json")
    public Object doLogin(String userName, String passWord, boolean remember, HttpSession session,
                          String ip, String address) {
        JSONObject json = new JSONObject();
        json.put(CommParams.LOGIN_CODE, 0);
        try {
            AuthenticationToken authenticationToken = remember ? new UsernamePasswordToken(userName, passWord, remember) : new UsernamePasswordToken(userName, passWord);
           /*shiro认证*/
            SecurityUtils.getSubject().login(authenticationToken);
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            session.setAttribute(CommParams.LOGIN_SESSION_KEY, user);
            /*写入登录日志*/
            LoginLog loginLog = new LoginLog();
            loginLog.setLoginDate(new Date());
            loginLog.setUserId(user.getUserId());
            loginLog.setIp(ip);
            loginLog.setLoginAddress(address);
            int item = sysBiz.addLoginLogBiz(loginLog);
            if (item==0){
                json.put(CommParams.LOGIN_MSG, "Login Error Code 1002 addLoginLog");
                return json.toString();
            }
            /*记录最后一次登录日期*/
            user.setLastLoginDate(DateTools.getCurrentDate(CommParams.DATE_FORMAT_STR_ONE));
            item = userBiz.modifyLastLoginDateBiz(user);
            if (item==0){
                json.put(CommParams.LOGIN_MSG, "Login Error Code 1002 modifyLastLoginDate");
                return json.toString();
            }
            json.put("token", session.getId());
            json.put(CommParams.LOGIN_MSG, "登录成功!");
            json.put(CommParams.LOGIN_CODE, 1);
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            json.put(CommParams.LOGIN_MSG, e.getMessage());
            return json.toString();
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            json.put(CommParams.LOGIN_MSG, e.getMessage());
            return json.toString();
        } catch (DisabledAccountException e) {
            e.printStackTrace();
            json.put(CommParams.LOGIN_MSG, e.getMessage());
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
            json.put(CommParams.LOGIN_MSG, e.getMessage());
            return json.toString();
        }
        return json.toString();
    }

    @GetMapping("/sendEmail.json")
    public Object sendEmail(String email){
        JSONObject json = new JSONObject();
        return json.toString();
    }

    @GetMapping("/sendSMS.json")
    public Object sendSMS(String phone){
        JSONObject json = new JSONObject();
        return json.toString();
    }
}
